const http = require('http')
const fs = require('fs')
const path = require('path')

const app = http.createServer((req, res) => {
    // 获取请求路径
    const { pathname } = new URL(req.url, 'http://127.0.0.1')

    if (pathname === '/') {
        const html = fs.readFileSync(path.join(__dirname, './file/index.html'))
        res.setHeader("Content-type", "text/html;charset=utf-8")
        res.end(html)
    } else if (pathname === '/style.css') {
        const css = fs.readFileSync(path.join(__dirname, './file/style.css'))
        res.end(css)
    } else if (pathname === '/index.js') {
        const js = fs.readFileSync(path.join(__dirname, './file/index.js'))
        res.end(js)
    } else {
        res.statusCode = 404
        res.setHeader("Content-type", "text/html;charset=utf-8")
        res.end('<h2>404 Not Found</h2>')
    }
})

app.listen(3000, () => {
    console.log('server is running...')
})